// LemCoop
// Copyright (C) 2013  Oliver Naumann <bldrax@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef LEMC_WORKMANAGER_H
#define LEMC_WORKMANAGER_H

#include <queue>
#include <mutex>

#include "job.h"

/*
* Implements queues for incoming and outgoing jobs.
*/
class WorkManager
{
private:
	// The queue of jobs for messages that have to be sent to a remote system.
	std::queue<Job> m_to_remote;

	// Mutex to protect the outgoing message queue.
	std::mutex m_remote_mutex;

	// The queue of jobs for messages that are received.
	std::queue<Job> m_to_local;

	// Mutex to protect the incoming message queue.
	std::mutex m_local_mutex;

public:
	/*
	* Puts a job into the outgoing message queue.
	*
	* @param job The job to push into the queue.
	*/
	void JobToRemote(Job job);

	/*
	* Puts a job into the incoming message queue.
	*
	* @param job The job to push into the queue.
	*/
	void JobToLocal(Job job);

	/*
	* Get the next job from the outgoing message queue.
	*
	* @param job The job into which the info is stored.
	*
	* @return True if there was a job in the queue, otherwise false.
	*/
	bool GetJobToRemote(Job& job);

	/*
	* Get the next job from the incoming message queue.
	*
	* @param job The job into which the info is stored.
	*
	* @return True if there was a job in the queue, otherwise false.
	*/
	bool GetJobToLocal(Job& job);
};

#endif